//
//  NewfeatureViewCtr.swift
//  SwiftStory
//
//  Created by ZxjMacBookPro on 2020/5/8.
//  Copyright © 2020 ZxjMacBookPro. All rights reserved.
//

import UIKit
import Masonry

class NewfeatureViewCtr: UIViewController {
    private var maxCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


}

extension NewfeatureViewCtr: UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        let index = collectionView.indexPathsForVisibleItems.last!
        NSLog(message: "indexPath.item = \(index.item)")

        let currentCell = collectionView.cellForItem(at: index) as! ZXJNewfeatureCell
        if index.item == (maxCount - 1){
            currentCell.startAniamtion()
        }

    }
}

extension NewfeatureViewCtr: UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewfeatureCell", for: indexPath) as! ZXJNewfeatureCell
        
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.red : UIColor.purple
        cell.index = indexPath.item
//        NSLog(message: "indexPath.item = \(indexPath.item)")

        return cell
    }
    
}

class ZXJNewfeatureCell: UICollectionViewCell {
    
    var index: Int = 0{
        didSet{
//            NSLog(message: "index = \(index)")
            let name = "new_feature_\(index + 1)"
            iconView.image = UIImage(named: name)
            startBtn.isHidden = true
        }
    }
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        setupUI()
    }

    func startAniamtion() {
        startBtn.isHidden = false
        /*
         withDuration：时间
         delay：延迟
         usingSpringWithDamping：震幅 0～1，越小震动越厉害
         initialSpringVelocity：加速度，值越大震动越厉害
         options：动画附加属性
         animations：执行动画
         completion：完成动画
         */
        startBtn.transform = CGAffineTransform.init(scaleX: 0.0, y: 0)
        startBtn.isUserInteractionEnabled = false
        UIView.animate(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIView.AnimationOptions(rawValue: 0), animations: {
            
            self.startBtn.transform = CGAffineTransform.identity
        }) { (_) in
            self.startBtn.isUserInteractionEnabled = true
        }
    }
    
    private func setupUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
//        iconView.backgroundColor = UIColor.white
//        iconView.translatesAutoresizingMaskIntoConstraints = false
        iconView.mas_makeConstraints { (make) in
//            make?.edges.equalTo()(contentView)
            make?.left.equalTo()(0)
            make?.right.equalTo()(0)
            make?.top.equalTo()(0)
            make?.bottom.equalTo()(0)
        }
        
        startBtn.mas_makeConstraints { (make) in
            make?.centerX.equalTo()(contentView)
            make?.bottom.equalTo()(contentView)?.offset()(-160)
        }
    }
    
    private lazy var iconView = UIImageView()
    
    lazy var startBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), for: .normal)
        btn.addTarget(self, action: #selector(startBtnClick), for: .touchUpInside)
        btn.sizeToFit()
        return btn
    }()

    @objc private func startBtnClick(){
        NotificationCenter.default.post(name: NSNotification.Name(ZXJSwitchRootViewController), object: true)
    }
}

class ZXJNewfeatureLayout: UICollectionViewFlowLayout {

    override func prepare() {
        itemSize = UIScreen.main.bounds.size
        
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        
        scrollDirection = .horizontal
        collectionView?.isPagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
    }
}
